package com.matt.controller.api.web;

import com.aliyun.oss.HttpMethod;
import com.dotflat.fin.utils.excelUtil.ExcelUtil;
import com.matt.commons.dto.sso.StaffDTO;
import com.matt.controller.po.ClienteleSortCountPO;
import com.matt.domain.Leads;
import com.matt.ext.aliyun.OSSProcessService;
import com.matt.foundation.model.StatusResponse;
import com.matt.foundation.utils.DateUtil;
import com.matt.foundation.vo.LeadsVo;
import com.matt.service.ClienteleService;
import com.matt.service.CrmCountService;
import com.matt.remoteCall.SsoCallService;
import com.matt.service.LeadsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2018/8/3
 * *******************************
 */
@RestController
@RequestMapping("/statistics/general")
@Api(tags = "CRM - 通用统计使用")
public class GeneralStatisticsController {

    @Autowired
    private LeadsService leadsService;

    @Autowired
    private ClienteleService clienteleService;

    @Autowired
    private CrmCountService crmCountService;

    @Autowired
    private SsoCallService ssoCallService;

    @Autowired
    private OSSProcessService ossProcessService;

    @Value("${aliyun.oss.fileServerPrefix}")
    private String IMG_SERVICE_PREFIX;
 

    //跟进客户数  [顾问个人每月跟进线索或客户的客户总数（同一客户只计算一次）（CRM数据）]
    @RequestMapping(value = "/follow_clientele_num")
    @ResponseBody
    @ApiOperation(value = "跟进客户数  [顾问个人每月跟进线索或客户的客户总数（同一客户只计算一次）（CRM数据）", httpMethod = "POST")
    public StatusResponse followClientleNum(@RequestHeader String ticket, String date){
        StaffDTO staff = ssoCallService.getStaffDTO(ticket);
        String staffId = staff.getId();
        Long count = crmCountService.followClienteleNum(staffId,date);
        return StatusResponse.success(count);
    }

    //本月签约所有合同数 [顾问个人每月签约的所有合同总数（包含正式合同与体验合同）（CRM数据）]
    @RequestMapping(value = "/sign_all_contract_num")
    @ApiOperation(value = "本月签约所有合同数 [顾问个人每月签约的所有合同总数（包含正式合同与体验合同）（CRM数据）]", httpMethod = "POST")
    public StatusResponse signAllContractNum(@RequestHeader String ticket,  String date){
        StaffDTO staff = ssoCallService.getStaffDTO(ticket);
        String staffId = staff.getId();
        Long count = crmCountService.signAllContractNum(staffId,date);
        return StatusResponse.success(count);
    }

    //签约正式合同数 [顾问个人每月签约正式合同总数（少于一个月的合同不计算入内）（CRM数据）]
    @RequestMapping(value = "/sign_formal_contract_num")
    @ApiOperation(value = "签约正式合同数 [顾问个人每月签约正式合同总数（少于一个月的合同不计算入内）（CRM数据）]", httpMethod = "POST")
    public StatusResponse signFormalContractNum(@RequestHeader String ticket, String date){
        StaffDTO staff = ssoCallService.getStaffDTO(ticket);
        String staffId = staff.getId();
        Long count = crmCountService.signFormalContractNum(staffId,date);
        return StatusResponse.success(count);
    }

    //新生数 [顾问个人每月签约的正式合同生效数（CRM数据）]
    @RequestMapping(value = "/sign_effective_contract_num")
    @ApiOperation(value = "新生数 [顾问个人每月签约的正式合同生效数（CRM数据）]", httpMethod = "POST")
    public StatusResponse signEffectiveContractNum(@RequestHeader String ticket, String date){
        StaffDTO staff = ssoCallService.getStaffDTO(ticket);
        String staffId = staff.getId();
        Long count = crmCountService.signEffectiveContractNum(staffId,date);
        return StatusResponse.success(count);
    }

    //面谈客户数 [顾问个人每月跟进状态为已到访的客户数（CRM数据）]
    @RequestMapping(value = "/view_clientele_num")
    @ApiOperation(value = "面谈客户数 [顾问个人每月跟进状态为已到访的客户数（CRM数据）]", httpMethod = "POST")
    public StatusResponse viewClienteleNum(@RequestHeader String ticket, String date){
        StaffDTO staff = ssoCallService.getStaffDTO(ticket);
        String staffId = staff.getId();
        Long count = crmCountService.viewClienteleNum(staffId,date);
        return StatusResponse.success(count);
    }

    //新客户数 [顾问每个月新增的客户总数（线索不算在内）（CRM数据）]
    @RequestMapping(value = "/increase_clientele_num")
    @ApiOperation(value = "新客户数 [顾问每个月新增的客户总数（线索不算在内）（CRM数据）]", httpMethod = "POST")
    public StatusResponse increaseClienteleNum(@RequestHeader String ticket, String date){
        StaffDTO staff = ssoCallService.getStaffDTO(ticket);
        String staffId = staff.getId();
        Long count = crmCountService.increaseClienteleNum(staffId,date);
        return StatusResponse.success(count);
    }

    //访客数 [前台每个月新增的“到店来访”线索数（CRM数据）]
    @RequestMapping(value = "/arrive_leads_num")
    @ApiOperation(value = "访客数 [前台每个月新增的“到店来访”线索数（CRM数据）]", httpMethod = "POST")
    public StatusResponse arriveLeadsNum(@RequestHeader String ticket, String date){
        StaffDTO staff = ssoCallService.getStaffDTO(ticket);
        String staffId = staff.getId();
        Long count = crmCountService.arriveLeadsNum(staffId,date);
        return StatusResponse.success(count);
    }

    //签约正式合同数（园区）[园区每月签约正式合同总数（少于一个月的合同不计算入内）（CRM数据）]
    @RequestMapping(value = "/area_contract_num")
    @ApiOperation(value = "签约正式合同数（园区）[园区每月签约正式合同总数（少于一个月的合同不计算入内）（CRM数据）]", httpMethod = "POST")
    public StatusResponse areaContractNum(@RequestHeader String ticket, String date){
        StaffDTO staff = ssoCallService.getStaffDTO(ticket);
        String areaId = staff.getAreaId();
        Long count = crmCountService.areaContractNum(areaId,date);
        return StatusResponse.success(count);
    }

    //合同履行中的客户数 [顾问个人本月合同正在履行中的合同数（CRM数据）]
    @RequestMapping(value = "/proceed_contract_num")
    @ApiOperation(value = "合同履行中的客户数 [顾问个人本月合同正在履行中的合同数（CRM数据）]", httpMethod = "POST")
    public StatusResponse proceedContractNum(@RequestHeader String ticket, String date){
        StaffDTO staff = ssoCallService.getStaffDTO(ticket);
        String staffId = staff.getId();
        Long count = crmCountService.proceedContractNum(staffId,date);
        return StatusResponse.success(count);
    }

    //客户宝宝入园数(顾问) [顾问所签约的客户宝宝每日的入园总数 (CRM数据)+（教务数据）]
    @RequestMapping(value = "/adviser_child_check_in_count")
    @ApiOperation(value = "客户宝宝入园数(顾问) [顾问所签约的客户宝宝每日的入园总数 (CRM数据)+（教务数据）]", httpMethod = "POST")
    public StatusResponse adviserChildCheckInCount(@RequestHeader String ticket, String date){
        StaffDTO staff = ssoCallService.getStaffDTO(ticket);
        String staffId = staff.getId();
        Long count = crmCountService.adviserChildCheckInCount(staffId,date);
        return StatusResponse.success(count);
    }

    //跟进数
    @RequestMapping(value = "/push_follow_count")
    @ApiOperation(value = "跟进数", httpMethod = "POST")
    public StatusResponse pushFollowNum(@RequestHeader String ticket){
        StaffDTO staff = ssoCallService.getStaffDTO(ticket);

        String staffId = staff.getId();

        return StatusResponse.success(crmCountService.pushFollowNum(staffId));
    }

    //顾问 周报数据
    @RequestMapping(value = "/get_adviser_week_data")
    @ApiOperation(value = "顾问 周报数据", httpMethod = "POST")
    public StatusResponse getAdviserWeekData(@RequestHeader String ticket, String date){
        StaffDTO staff = ssoCallService.getStaffDTO(ticket);
        String staffId = staff.getId();
        return StatusResponse.success(crmCountService.getAdviserWeekData(staffId, staffId, date));
    }

    //园区 周报数据
    @RequestMapping(value = "/get_area_week_data")
    @ApiOperation(value = "园区 周报数据", httpMethod = "POST")
    public StatusResponse getAreaWeekData(@RequestHeader String ticket, String date){
        StaffDTO staff = ssoCallService.getStaffDTO(ticket);
        String staffId = staff.getId();
        return StatusResponse.success(crmCountService.getAreaWeekData(staffId, date));
    }

    //按月统计园区crm线索到店情况 渠道分布，转化客户率，跟进率
    @RequestMapping(value = "/clientele_sort_count")
    @ApiOperation(value = "线索分类统计", httpMethod = "GET", response = Integer.class)
    public StatusResponse clienteleSortCount(String areaIds,String date) throws  Exception{

        List<ClienteleSortCountPO> list  = crmCountService.clienteleSortCount(areaIds,date);
        ExcelUtil<ClienteleSortCountPO> util = new ExcelUtil<ClienteleSortCountPO>(ClienteleSortCountPO.class);


        File outputFile = new File(util.exportExcel(list, "参数数据") );
        Calendar cal = Calendar.getInstance();
        cal.setTime(DateUtil.parseToDateHour(date));
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;

        String uploadPath = "teach/file/children_export_excel/" + DateTime.now().toString("yyyy-MM") + "/"+year+"年"+month+"月"+"线索统计报表.xlsx" ;
        String relativePath = ossProcessService.aliyunOssUpload(uploadPath, outputFile);
        String uploadedResult = IMG_SERVICE_PREFIX + relativePath;

        outputFile.deleteOnExit();
        outputFile.delete();
        return StatusResponse.success(uploadedResult);
    }

}
